package com.itheima.controller;


import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 学生列表查询
     * 根据学生列表数据的条件分页查询
     *
     * @return
     */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("分页查询学生列表，参数：{}", studentQueryParam);
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 删除学生
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam("/{ids}") List<Integer> ids) {
        log.info("删除学生，id：{}", ids);
        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 新增学生
     *
     * @param student
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Student student) {
        log.info("新增学生，参数：{}", student);
        studentService.add(student);
        return Result.success();
    }

    /**
     * 根据id查询员工
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询员工: {}", id);
        Student student = studentService.selectById(id);
        return Result.success(student);
    }

    /**
     * 修改员工信息
     *
     * @param student
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改员工信息，参数：{}", student);
        studentService.update(student);
        return Result.success();
    }
    /**
     * 违纪处理
     *
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violate(@PathVariable Integer id, @PathVariable Integer score){
        log.info("违纪处理，参数：{}, {}", id, score);
        studentService.violate(id, score);
        return Result.success();
    }
}
